#include<stdio.h>
#include<malloc.h>

typedef struct stack Stack;

struct stack{
	int capacity;
	int top;
	int *arr;
};

Stack *CreateStack(int);
int StackPush(Stack *, int);
int StackPop(Stack *);
int isStackFull(Stack *);
int isStackEmpty(Stack *);

int main(){
	Stack *s; int i=0;
	s = CreateStack(5);

	for(i=0;i<6;i++){
		if(StackPush(s, i)){
			printf("\nPushed %d to the stack successfully", i);
		}else{
			printf("\nPushing %d to the stack failed!", i);
		}
	}

	printf("\n---Debug---\nPrinting Stack!\n");
	while(!isStackEmpty(s)){
		printf("\nPopping %d", StackPop(s));
	}

	printf("\n--Stack--\n");
	return 0;
}


Stack *CreateStack(int cap){
	Stack *S = (Stack *)malloc(sizeof(Stack));
	if(!S){
		return NULL;
	}	
	S->capacity = cap;
	S->top = 0;
	S->arr = (int *)malloc(cap * sizeof(int));
	if(!S->arr){
		free(S);
		return NULL;
	}
	
	return S;
}

int StackPush(Stack *S, int data){
	if(isStackFull(S)){
		return 0;
	}else{
		S->arr[S->top] = data;
		S->top++;
		return 1;
	}
}

int StackPop(Stack *S){
	if(isStackEmpty(S)){
		return 0;
	}else{
		--S->top;
		return S->arr[S->top];
	}
}

int isStackFull(Stack *S){
	return (S->top == S->capacity);
}

int isStackEmpty(Stack *S){
	return (S->top == 0);
}
